package com.ihrm.common.realm;

import com.ihrm.domain.system.response.ProfileResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Set;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.ihrm.common.realm
 * @date 2020/12/21 18:37
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 */
public class IhrmRealm extends AuthorizingRealm {
    @Override
    public void setName(String name) {
        super.setName("ihrmRealm");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取认证用户信息
        ProfileResult user =(ProfileResult) principalCollection.getPrimaryPrincipal();
        //构建用户权限集合
        Set<String> apis = (Set<String>) user.getRoles().get("apis");
        //添加用户权限和角色
        //构造返回结果
        SimpleAuthorizationInfo info= new SimpleAuthorizationInfo();
        info.setStringPermissions(apis);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}
